package com.cneiu.ppms.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cneiu.ppms.web.base.BaseController;
import com.cneiu.ppms.web.model.LineModel;
import com.cneiu.ppms.web.service.LineService;
import com.jfinal.plugin.activerecord.Page;
/**
 * 供电线路Controller
 * @author zhaokf
 * @date 2013-04-19
 */
public class LineController extends BaseController
{

	/**
	 * 获取机构列表（ComboBox树形）
	 */
	public void treeComboBox(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		LineModel model =LineModel.dao.findFirst("select * from g_line where line_id = ?", node);
		List<HashMap<Object,Object>> mapList =  null;
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		mapList = LineService.treeComboBox(model);
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, LineService.getError());
		}
	}
	
	
	
	/**
	 * 获取机构列表（树形）
	 */
	public void treeList(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		List<LineModel> modelList = LineModel.dao.find("select * from g_line where line_id = ?", node);
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		List<HashMap<Object,Object>> mapList = new ArrayList<HashMap<Object,Object>>();
		for(LineModel model:modelList){
			mapList.add(LineService.treeList(model));
		}
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, LineService.getError());
		}
	}
	/**
	 * 获取page limit得到供电线路列表
	 */
	public void list(){
	
		Page<LineModel> Lines = LineService.list(this.getParams());
		if (null != Lines){
			renderGrid(Lines.getTotalRow(), Lines.getList());
		}else{
			renderAjax(false, LineService.getError());
		}
	}

	/**
	 * 添加供电线路
	 */
	public void add(){
		try{
			boolean isNotError = true;
			if (!LineService.isAdd(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, LineService.getError());
		}
		catch (Exception e){}

	}

	/**
	 * 编辑供电线路
	 */
	public void edit(){
		try{
			boolean isNotError = true;
			if (LineService.isUpdate(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, LineService.getError());
		}
		catch (Exception e){}
	}
	
	/**
	 * 删除供电线路
	 */
	public void delete(){
		try{
			boolean isNotError = true;
			if (LineService.isDelete(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, LineService.getError());
		}
		catch (Exception e){
		}
	}
}
